package www.dianbaozu.com.ui.chat.findFriend

import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.netease.nim.uikit.api.NIMRepository
import com.netease.nimlib.sdk.friend.model.Friend
import com.netease.nimlib.sdk.msg.model.IMMessage
import com.netease.nimlib.sdk.search.model.MsgIndexRecord
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
import www.dianbaozu.com.R
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.LoadingDialog
import www.wen.com.common.lib.data.entities.LoadingDialogStateType
import www.wen.com.common.lib.data.entities.Result
import www.wen.com.common.lib.data.entities.UiStateCustom
import www.wen.com.common.lib.extension.getString

class FindFriendsMessagesViewModel @ViewModelInject constructor() :
    BaseViewModel() {

    val searchString = MutableLiveData("")

    @FlowPreview
    fun searchFriend(
        keyword: String,
        success: ((Result.Success<List<Friend>>) -> Unit),
    ) {
        viewModelScope.launch {
            flow {
                try {
                    emit(Result.Success(NIMRepository.searchFriend(keyword)))
                } catch (e: Exception) {
//                    Result.Error(e)
                    throw e
                }
            }.retryWhen { cause, attempt ->
                return@retryWhen cause.message == "网络异常" && attempt <= 1
            }
//                .onStart {
//                    loading.value = UiStateCustom(
//                        stateData = LoadingDialog(
//                            getString(R.string.loading),
//                            LoadingDialogStateType.LOADING
//                        )
//                    )
//                }
//                .onCompletion { cause ->
//                    if (cause == null) {
//                        loading.value = UiStateCustom(
//                            stateData = LoadingDialog(
//                                tip = "",
//                                loadingDialogStateType = LoadingDialogStateType.SUCCESS,
//                                isShowDialogTip = false,
//                                isShowToastTip = false
//                            )
//                        )
//                    }
//                }
                .catch { cause ->
                    loading.value = UiStateCustom(
                        stateData = LoadingDialog(
                            tip = cause.message ?: "网络异常",
                            loadingDialogStateType = LoadingDialogStateType.ERROR,
                            isShowDialogTip = false,
                            isShowToastTip = true
                        )
                    )
                }
                .collect {
                    success.invoke(it)
                }
        }
    }

    @FlowPreview
    fun searchAllMessage(
        keyword: String,
        success: ((Result.Success<List<MsgIndexRecord>>) -> Unit),
    ) {
        viewModelScope.launch {
            flow {
                try {
                    emit(Result.Success(NIMRepository.searchAllMessage(keyword)))
                } catch (e: Exception) {
//                    Result.Error(e)
                    throw e
                }
            }.retryWhen { cause, attempt ->
                return@retryWhen cause.message == "网络异常" && attempt <= 1
            }
                .catch { cause ->
                    loading.value = UiStateCustom(
                        stateData = LoadingDialog(
                            tip = cause.message ?: "网络异常",
                            loadingDialogStateType = LoadingDialogStateType.ERROR,
                            isShowDialogTip = false,
                            isShowToastTip = true
                        )
                    )
                }
                .collect {
                    success.invoke(it)
//                    if (it.data.isNotEmpty() && inputChanged.value == false) {
//                        delay(300)
//                        queryMessageListEx(it.data.last(), success, error)
//                    }
                }
        }
    }

}